package com.bw.common;

import lombok.Data;

import java.io.Serializable;
/**
 * @author 13096
 */
@Data
public class ResultModel<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    /**
     * 成功
     * @param data
     * @return
     */
    public static<T> ResultModel<T> successed(T data){
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.setCode(200);
        resultModel.setMessage("操作成功");
        resultModel.setData(data);
        return resultModel;
    }

    /**
     * 失败
     * @param errorEnums
     * @return
     */
    public static<T> ResultModel<T> failed(ErrorEnums errorEnums){
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.setCode(errorEnums.getCode());
        resultModel.setMessage(errorEnums.getMessage());
        resultModel.setData(null);
        return resultModel;
    }
    public static<T> ResultModel<T> failed(Integer code,String message){
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.setCode(code);
        resultModel.setMessage(message);
        resultModel.setData(null);
        return resultModel;
    }
    /**
     * 处理异常
     * @param data
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static<T> ResultModel<T> exception(T data,Integer code,String message){
        ResultModel<T> resultModel = new ResultModel<T>();
        resultModel.data = data;
        resultModel.code = code;
        resultModel.message = message;
        return resultModel;
    }
}
